﻿using MapleLeaf.Application.CookBook.Models;
using MapleLeaf.Core.Cron;
using MapleLeaf.Core.Dto;
using MapleLeaf.Core.Extension;
using MapleLeaf.Model.CookBook.Menu.Entity;
using MapleLeaf.Model.CookBook.Organization.Entity;
using MapleLeaf.Service.CookBook.Menu;
using MapleLeaf.Service.CookBook.Organization;
using Mapster;
using SqlSugar;
using System.Linq.Expressions;

namespace MapleLeaf.Application.CookBook {
    /// <summary>
    /// 系统设置
    /// </summary>  
    public class SystemClient : IApplicationClient {
        private readonly SystemMenuService _menuService;
        private readonly DictService _dictService;
        private readonly DictItemService _dictItemService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SystemClient(SystemMenuService menuService, DictService dict, DictItemService dictItem) {
            _menuService = menuService;
            _dictService = dict;
            _dictItemService = dictItem;
        }


        #region 菜单管理
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public List<string> GetMenuPermissionList(string roleCode) {
            var permissionList = _menuService.GetMenuPermissionList(roleCode);
            return permissionList;
        }

        /// <summary>
        /// 获取菜单管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<PaginationModel<MenuVo>> GetMenuPageListAsync(Expression<Func<MenuEntity, bool>> whereExp, PaginationModel pagination) {
            var page = pagination.Adapt<PaginationModel<MenuVo>>();
            RefAsync<int> total = 0;
            var res = await _menuService.GetPageListAsync(whereExp, page.PageIndex, page.PageSize, total);
            page.Total = total;
            page.List = res.Adapt<List<MenuVo>>();
            return page;
        }
        /// <summary>
        /// 获取菜单管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<MenuEntity>> GetMenuListAsync(Expression<Func<MenuEntity, bool>> whereExp) {
            var res = await _menuService.GetListAsync(whereExp);
            res = [.. res.OrderBy(x => x.Sort)];
            return res;
        }
        /// <summary>
        /// 获取菜单管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<MenuEntity> GetMenuInfoAsync(long id) {
            var res = await _menuService.GetByIdAsync(id);
            return res;
        }
        /// <summary>
        /// 保存菜单管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<long> SaveMenuAsync(MenuEntity entity) {
            long id;
            if (entity.Component.IsNullOrEmpty() && entity.Type == 2) {
                entity.Component = "Layout";
            }
            if (entity.Id == 0) {
                var res = await _menuService.InsertReturnEntityAsync(entity);
                id = res != null ? res.Id : 0;
            } else {
                var res = await _menuService.UpdateAsync(entity);
                id = res ? entity.Id : 0;
            }
            return id;
        }
        /// <summary>
        /// 删除菜单管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<bool> DeleteMenuAsync(List<long> ids) {
            var res = await _menuService.DeleteByIdAsync(ids);
            return res > 0;
        }
        #endregion
        #region 字典管理
        /// <summary>
        /// 获取字典管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictEntity>> GetDictPageListAsync(Expression<Func<DictEntity, bool>> whereExp, PageModel pagination) {
            var res = await _dictService.GetPageListAsync(whereExp, pagination);
            return res;
        }
        /// <summary>
        /// 获取字典管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictEntity>> GetDictListAsync(Expression<Func<DictEntity, bool>> whereExp) {
            var res = await _dictService.GetListAsync(whereExp);
            return res;
        }
        /// <summary>
        /// 获取字典管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<DictEntity> GetDictInfoAsync(long id) {
            var res = await _dictService.GetByIdAsync(id);
            return res;
        }
        /// <summary>
        /// 保存字典管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<long> SaveDictAsync(DictEntity entity) {
            long id;
            if (entity.Id == 0) {
                var res = await _dictService.InsertReturnEntityAsync(entity);
                id = res != null ? res.Id : 0;
            } else {
                var res = await _dictService.UpdateAsync(entity);
                id = res ? entity.Id : 0;
            }
            return id;
        }
        /// <summary>
        /// 删除字典管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<bool> DeleteDictAsync(List<long> ids) {
            var res = await _dictService.DeleteByIdAsync(ids);
            return res > 0;
        }
        #endregion
        #region 字典项管理
        /// <summary>
        /// 获取字典项管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictItemEntity>> GetDictItemPageListAsync(Expression<Func<DictItemEntity, bool>> whereExp, PageModel pagination) {
            var res = await _dictItemService.GetPageListAsync(whereExp, pagination);
            return res;
        }
        /// <summary>
        /// 获取字典项管理列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictItemEntity>> GetDictItemListAsync(Expression<Func<DictItemEntity, bool>> whereExp) {
            var res = await _dictItemService.GetListAsync(whereExp);
            return res;
        }
        /// <summary>
        /// 获取字典项管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<DictItemEntity> GetDictItemInfoAsync(long id) {
            var res = await _dictItemService.GetByIdAsync(id);
            return res;
        }
        /// <summary>
        /// 保存字典项管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<long> SaveDictItemAsync(DictItemEntity entity) {
            long id;
            if (entity.Id == 0) {
                var res = await _dictItemService.InsertReturnEntityAsync(entity);
                id = res != null ? res.Id : 0;
            } else {
                var res = await _dictItemService.UpdateAsync(entity);
                id = res ? entity.Id : 0;
            }
            return id;
        }
        /// <summary>
        /// 删除字典项管理信息
        /// </summary>
        /// <returns></returns>
        public async Task<bool> DeleteDictItemAsync(List<long> ids) {
            var res = await _dictItemService.DeleteByIdAsync(ids);
            return res > 0;
        }
        #endregion
    }
}
